//
//  PGExternal.m
//  PhoneGepTest
//
//  Created by 경서 박 on 12. 1. 3..
//  Copyright (c) 2012년 __MyCompanyName__. All rights reserved.
//

#import "PGExternal.h"
#import <Cordova/JSONKit.h>

@interface PGExternal (Private)
- (void)sendJson:(NSString *)jsonStr;
@end

@implementation PGExternal

- (void)getContact:(NSMutableArray *)arg withDict:(NSDictionary *)options
{
    NSString *id = [[arg pop] copy];
    callbackID = [[NSString stringWithString:id] retain];
    NSLog(@"callbackID = %@", callbackID);
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(sendAddress:) name:kPGResultAddress object:nil];
    [[NSNotificationCenter defaultCenter] postNotificationName:kPGGetAddr object:self];
}
- (void)sendBedgeCnt:(NSMutableArray *)arg withDict:(NSDictionary *)options
{
    NSLog(@"arg = %@ %@", arg, options);
    [[UIApplication sharedApplication] setApplicationIconBadgeNumber:[[options valueForKey:@"count"] intValue]];
}
- (void)pushCallBack:(NSMutableArray *)arg withDict:(NSDictionary *)options
{
    NSString *id = [[arg pop] copy];
    pushCallBackID = [[NSString stringWithString:id] retain];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(pushNoti:) name:kPGPushNotification object:nil];
}
- (void)pushNoti:(NSNotification *)notification
{
    NSDictionary *dic = [notification userInfo];
    
    CDVPluginResult *pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsDictionary:dic];
    
    [self writeJavascript: [pluginResult toSuccessCallbackString:pushCallBackID]];
}
- (void)sendAddress:(NSNotification *)notification
{
    NSDictionary *dic = [notification userInfo];
//    NSDictionary *returnDic = [NSDictionary dictionaryWithDictionary:[dic objectForKey:@"userInfo"]];
    NSLog(@"dic = %@", [dic objectForKey:@"name"]);
    CDVPluginResult *pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsDictionary:dic];
    
    [self writeJavascript: [pluginResult toSuccessCallbackString:callbackID]];
}
- (void)dealloc
{
    [super dealloc];
}
@end
